/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.beans;

import it.nyquistmeteocal.entity.Event;
import it.nyquistmeteocal.boundary.UserManager;
import it.nyquistmeteocal.entity.CalendarEvents;
import it.nyquistmeteocal.boundary.CalendarEventsManager;
import it.nyquistmeteocal.boundary.EventManager;
import it.nyquistmeteocal.entity.Notification;
import it.nyquistmeteocal.boundary.NotificationManager;
import it.nyquistmeteocal.entity.UserNotification;
import it.nyquistmeteocal.boundary.UserNotificationManager;
import java.awt.event.ActionEvent;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author gatb27
 */
@Named(value = "createBean")
@RequestScoped
public class CreateEventBean {

    @EJB
    private EventManager em;
    @EJB
    private UserManager um;
    @EJB
    private CalendarEventsManager cm;
    @EJB
    private NotificationManager nm;
    @EJB
    private UserNotificationManager unm;

    private Event event;
    private String creator;
    private Integer eventid = 0;
    private CalendarEvents calendarevents = new CalendarEvents();
    private Notification notification = new Notification();
    private String text;
    private String eventname;
    private Integer notificationid = 0;
    private UserNotification usernotification = new UserNotification();
    private String eventdate;
    private String weather;
    private WeatherBean wb = new WeatherBean();

    ///code for invites 5 users
    private String invited1 = "email";
    private String invited2 = "email";
    private String invited3 = "email";
    

    public String getInvited1() {
        return invited1;
    }

    public void setInvited1(String invited1) {
        this.invited1 = invited1;
    }

    public String getInvited2() {
        return invited2;
    }

    public void setInvited2(String invited2) {
        this.invited2 = invited2;
    }

    public String getInvited3() {
        return invited3;
    }

    public void setInvited3(String invited3) {
        this.invited3 = invited3;
    }

   ///end of code for invite 5 users
    public CreateEventBean() {
    }

    /*get and set for Event for createBean*/
    public Event getEvent() {
        if (event == null) {
            event = new Event();
        }
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;

    }

    /*get and set for UserNotification for createBean*/
    public UserNotification getUserNotification() {
        if (usernotification == null) {
            usernotification = new UserNotification();
        }
        return usernotification;
    }

    public void setUserNotification(UserNotification usernotification) {
        this.usernotification = usernotification;

    }

    public String saveEvent() throws IOException {
        
        /*
        String sURL = "http://api.openweathermap.org/data/2.5/weather?q=";
        sURL = sURL + this.event.getCity();

        // Connessione all'url per ricevere lo stream contenente il file json richiesto
        URL url = new URL(sURL);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.connect();

        // Conversione dello stream di input in un oggetto json
        JsonParser jp = new JsonParser();
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));

        // Ricerca del meteo all'interno dell'oggetto json
        JsonObject rootobj = root.getAsJsonObject();
        this.weather = rootobj.get("weather").getAsJsonArray().get(0).getAsJsonObject().get("main").getAsString();
        System.out.println("the weather forecast for the city IS: " +  this.weather);
        */
        
        this.weather = wb.weatherWithCityDate(this.event.getCity(), this.event.getEventdate());

        /*obtain the creator's email*/
        creator = um.getLoggedUser().getEmail();
        this.event.setCreator(this.creator);
        this.event.setWeather(this.weather);

        /*save information in db event*/
        em.saveEvent(event);

        /*save information in db calendar*/
        System.out.println("event id before get is:   " + eventid);
        eventid = event.getId();
        System.out.println("event id after get is:   " + eventid);
        System.out.println("event id after get is:   " + creator);

        this.calendarevents.setEventId(this.eventid);
        System.out.println("eventid in calendar could be:   " + this.calendarevents.getEventId());
        this.calendarevents.setUserEmail(this.creator);
        System.out.println("useremail in calendar could be:   " + this.calendarevents.getUserEmail());

        /*save information in db calendarevents*/
        cm.saveCalendarEvents(calendarevents);

        if (!invited1.equals("email") || !invited2.equals("email") || !invited2.equals("email")) {
            /*  RELATED TO INVITE NOTIFICATION*/
            /*get the event*/
            eventname = this.event.getEventname();
            System.out.println("the event name of the current event is:   " + eventname);
            /*define the text for the notification*/
            eventdate = this.event.getEventdate();
            text = "You've been invited to " + eventname + " from " + this.creator + " on " + this.eventdate;
            System.out.println("text of the notification is:   " + text);
            /*set the text for the notification*/
            this.notification.setText(this.text);
            System.out.println("text of notification after get:  " + this.notification.getText());
            /*set the type for the notification, here invite for default*/
            this.notification.setType("invite");
            System.out.println("type of notification after get:   " + this.notification.getType());
            /*set the eventid for the notification*/
            this.notification.setEventid(this.eventid);

            /*save information in db notification*/
            nm.saveNotification(notification);

            /*get id notification just created*/
            notificationid = this.notification.getId();
            System.out.println("not id is " + notificationid);
            System.out.println("first invited email " + this.invited1);

            /*set the not id in usnoti*/
            this.usernotification.setNotificationid(this.notificationid);
            System.out.println("not id is" + this.usernotification.getNotificationid());

            /*save information in db usernotification*/
            ///unm.saveUserNotification(usernotification);
            if (!invited1.equals("email")) {
                this.usernotification.setUsermail(invited1);
                unm.saveUserNotification(usernotification);
            }

            if (!invited2.equals("email")) {
                this.usernotification.setUsermail(invited2);
                unm.saveUserNotification(usernotification);
            }

            if (!invited3.equals("email")) {
                this.usernotification.setUsermail(invited3);
                unm.saveUserNotification(usernotification);
            }

        }

        /*next page to visualize*/
        return "displayEvent";
    }

}
